﻿#include "bloodpressure.h"
#include<QPushButton>

class TimeScaleDraw:public QwtScaleDraw
{
public:
    virtual QwtText label(double v)const
    {
        QDateTime time=QDateTime::fromMSecsSinceEpoch(v);
        return  time.toString("hh:mm:ss\nyy-MM-dd");

    }
};

BloodPressure::BloodPressure(QwtPlot *plot, QLabel *hbp, QLabel *lbp, QLabel *pulse):QObject(plot)
{
    m_BPPlot=plot;
    m_hbp=hbp;
    m_lbp=lbp;
    m_pulse=pulse;
    m_testData=new TestData();
    m_testData->pHBPData=new PlotData();
    m_testData->pLBPData=new PlotData();
    m_testData->pPulseData=new PlotData();
    m_serial=new SerialTool();
}

void BloodPressure::setParam(int sampleRate, double xWidth)
{
    m_testData->pHBPData->setParam(sampleRate,xWidth);
    m_testData->pLBPData->setParam(sampleRate,xWidth);
    m_testData->pPulseData->setParam(sampleRate,xWidth);
}

void BloodPressure::buildPlot()
{
    initPlot();
    addPlotGrid();
    addLegend();
    addManifier();
    addPanner();
    addCurve();
}

void BloodPressure::initPlot()
{
    m_BPPlot->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw);
    //范围
    m_BPPlot->setAxisScale(QwtPlot::yLeft,0,160);
    m_BPPlot->setAxisScale(QwtPlot::xBottom,m_testData->interval.minValue(),m_testData->interval.maxValue());
}

void BloodPressure::addPlotGrid()
{
    QwtPlotGrid*grid=new QwtPlotGrid();
    grid->setPen(Qt::gray,0.0,Qt::DotLine);
    grid->enableY(true);
    grid->enableYMin(true);
    grid->enableX(true);
    grid->enableXMin(true);
    grid->attach(m_BPPlot);
}
//缩放
void BloodPressure::addManifier()
{
    m_testData->pManifier=new QwtPlotMagnifier(m_BPPlot->canvas());
    m_testData->pManifier->setAxisEnabled(QwtPlot::yLeft,false);
}
//显示名称
void BloodPressure::addLegend()
{
    m_BPPlot->insertLegend(new QwtLegend(),QwtPlot::RightLegend);
}
//拖拽
void BloodPressure::addPanner()
{
    m_testData->pPanner=new QwtPlotPanner(m_BPPlot->canvas());
    m_testData->pPanner->setOrientations(Qt::Horizontal);
}

void BloodPressure::drawHistoryData(quint64 ulStartTime, QVector<unsigned char> vctHBP, QVector<unsigned char> vctLBP, QVector<unsigned char> vctPulse)
{
    m_testData->pHBPData->addBaseData(ulStartTime,vctHBP);
    m_testData->pLBPData->addBaseData(ulStartTime,vctLBP);
    m_testData->pPulseData->addBaseData(ulStartTime,vctPulse);
    //同时进行取其一
    QwtInterval interval=m_testData->pHBPData->getXInterval();
    m_testData->interval=interval;
    m_BPPlot->setAxisScale(QwtPlot::xBottom,m_testData->interval.minValue(),m_testData->interval.maxValue());
    m_BPPlot->replot();
}

void BloodPressure::addCurve()
{
    m_testData->pLBPCurve=new QwtPlotCurve("LBP");
    m_testData->pLBPCurve->setStyle(QwtPlotCurve::Lines);
    m_testData->pLBPCurve->setPen(QColor("#6abf31"),3);
    m_testData->pLBPCurve->setData(m_testData->pLBPData);

    m_testData->pHBPCurve=new QwtPlotCurve("HBP");
    m_testData->pHBPCurve->setStyle(QwtPlotCurve::Lines);
    m_testData->pHBPCurve->setPen(QColor("#F40200"),3);
    m_testData->pHBPCurve->setData(m_testData->pHBPData);

    m_testData->pPulseCurve=new QwtPlotCurve("Pulse");
    m_testData->pPulseCurve->setStyle(QwtPlotCurve::Lines);
    m_testData->pPulseCurve->setPen(QColor("#EEFF66"),3);
    m_testData->pPulseCurve->setData(m_testData->pPulseData);
    //放入曲线
    m_testData->pHBPCurve->attach(m_BPPlot);
    m_testData->pLBPCurve->attach(m_BPPlot);
    m_testData->pPulseCurve->attach(m_BPPlot);
}

//串口处理
void BloodPressure::serialPortInit()
{
    //等待Serial完成
    m_serial->m_serialport=new QSerialPort();
    connect(m_serial->m_serialport,SIGNAL(readyRead()),this,SLOT(receiveData()));
    m_serial->serialOpen();
}

void BloodPressure::receiveData()
{
    QByteArray message;
    message.append(m_serial->m_serialport->readAll());
    updateCurve(message);
}

void BloodPressure::updateCurve(QByteArray message)
{
    m_testData->pHBPData->updateCurveData(message.at(0));
    m_testData->pLBPData->updateCurveData(message.at(1));
    m_testData->pPulseData->updateCurveData(message.at(2));
    m_testData->interval=m_testData->pHBPData->getXInterval();
    m_BPPlot->setAxisScale(QwtPlot::xBottom,m_testData->interval.minValue(),m_testData->interval.maxValue());
    m_BPPlot->updateAxes();
    //填入数值
    unsigned char num=message.at(0);
    m_hbp->setText(QString::number(num,10));
    num=message.at(1);
    m_lbp->setText(QString::number(num,10));
    num=message.at(2);
    m_pulse->setText(QString::number(num,10));
    m_BPPlot->replot();
}

